package com.szs.controller;

import com.szs.pojo.*;
import com.szs.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    /*
    * 分页查询学员列表
    * */
    @GetMapping
    public Result studentsPage(StudentQueryParam studentQueryParam){
        log.info("分页查询：{}",studentQueryParam);
        PageResult<Student> pageResult = studentsService.studentsPage(studentQueryParam);
        return Result.success(pageResult);
    }
    /*
    * 根据ID查询学生信息
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询学生：{}",id);
        Student student = studentsService.getById(id);
        return Result.success(student);
    }

    /*
    * 新增学生信息 json格式请求参数
    * */
    @PostMapping
    public Result saveStudent(@RequestBody Student student){
        log.info("新增学员信息：{}",student);
        studentsService.save(student);
        return Result.success();
    }

    /*
    * 修改学员信息 json格式请求参数
    * */
    @PutMapping
    public Result updateStu(@RequestBody Student student){
        log.info("修改学员信息：{}",student);
        studentsService.updateStu(student);
        return Result.success();
    }

    /*
    * 根据id删除学生信息
    * */
    @DeleteMapping("/{ids}")
    public Result deleteStu( @PathVariable String ids){
        // 将逗号分隔的字符串转换为 List<Integer>
        List<Integer> idsList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        log.info("删除员工：{}",idsList);
        studentsService.delete(idsList);
        return Result.success();
    }

    /*
    * 修改学生违纪处理
    * */
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("修改学生违纪处理:{},{}",id,score);
        studentsService.updateVio(id,score);
        return Result.success();
    }
}
